package mil.af.amc.adpetracker.web.actions.struts1.EquipmentItem.search;

import mil.af.amc.adpetracker.web.actions.struts1.AbstractAction;
import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.model.EquipmentItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class EquipmentItemSearchAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(EquipmentItemSearchAction.class);

    private EquipmentItemService equipmentItemService;

    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {

        DynaActionForm form = (DynaActionForm) actionForm;

        String searchPattern = (String) form.get("searchPattern");

        // Here I go again, breaking my rule of no model objects in the controller layer...
        List<EquipmentItem> searchResults = equipmentItemService.luceneSearch(searchPattern);

        httpServletRequest.setAttribute("searchResults", searchResults);

        return actionMapping.findForward("success");
    }
}
